#include "groundtestwidget.h"

QGroundTestWidget::QGroundTestWidget(QWidget *parent) : QWidget(parent)
{
    st_workmodeParam l_param;
    l_param.sName = "累加运动模式";
    l_param.paramCnt = 1;
    l_param.param1.sParamName = "累加值";
    l_param.param1.lfCoeff = 1;
    l_param.param1.lfParamMin = -32768;
    l_param.param1.lfParamMax = 32767;
    l_param.param1.sUnit = "urad";
    _workModeParamMap[0x7A] = l_param;

    l_param.sName = "正弦扫频模式";
    l_param.paramCnt = 2;
    l_param.param1.sParamName = "扫描频率";
    l_param.param1.lfCoeff = 1000;
    l_param.param1.lfParamMin = 0.1;
    l_param.param1.lfParamMax = 5;
    l_param.param1.sUnit = "Hz";
    l_param.param2.sParamName = "扫频幅度";
    l_param.param2.lfCoeff = 1;
    l_param.param2.lfParamMin = 0;
    l_param.param2.lfParamMax = 5;
    l_param.param2.sUnit = "°";
    _workModeParamMap[0x7B] = l_param;

    l_param.sName = "阶跃模式";
    l_param.paramCnt = 2;
    l_param.param1.sParamName = "扫描频率";
    l_param.param1.lfCoeff = 1000;
    l_param.param1.lfParamMin = 0.1;
    l_param.param1.lfParamMax = 5;
    l_param.param1.sUnit = "Hz";
    l_param.param2.sParamName = "扫频幅度";
    l_param.param2.lfCoeff = 1;
    l_param.param2.lfParamMin = 0;
    l_param.param2.lfParamMax = 65535;
    l_param.param2.sUnit = "°";
    _workModeParamMap[0x7C] = l_param;


    l_param.sName = "电流环模型辨识";
    l_param.paramCnt = 2;
    l_param.param1.sParamName = "扫描频率";
    l_param.param1.lfCoeff = 10;
    l_param.param1.lfParamMin = 50;
    l_param.param1.lfParamMax = 5000;
    l_param.param1.sUnit = "Hz";
    l_param.param2.sParamName = "扫描幅度";
    l_param.param2.lfCoeff = 1000;
    l_param.param2.lfParamMin = 0;
    l_param.param2.lfParamMax = 3;
    l_param.param2.sUnit = "A";
    _workModeParamMap[0x8A] = l_param;

    l_param.sName = "速度环模型辨识";
    l_param.paramCnt = 2;
    l_param.param1.sParamName = "扫描频率";
    l_param.param1.lfCoeff = 10;
    l_param.param1.lfParamMin = 0.1;
    l_param.param1.lfParamMax = 1000;
    l_param.param1.sUnit = "Hz";
    l_param.param2.sParamName = "扫描幅度";
    l_param.param2.lfCoeff = 1000;
    l_param.param2.lfParamMin = 0;
    l_param.param2.lfParamMax = 5;
    l_param.param2.sUnit = "°/s";
    _workModeParamMap[0x8B] = l_param;

    l_param.sName = "位置环模型辨识";
    l_param.paramCnt = 2;
    l_param.param1.sParamName = "扫描频率";
    l_param.param1.lfCoeff = 10;
    l_param.param1.lfParamMin = 0.1;
    l_param.param1.lfParamMax = 100;
    l_param.param1.sUnit = "Hz";
    l_param.param2.sParamName = "扫描幅度";
    l_param.param2.lfCoeff = 1000;
    l_param.param2.lfParamMin = 0;
    l_param.param2.lfParamMax = 10;
    l_param.param2.sUnit = "";
    _workModeParamMap[0x8C] = l_param;

    _i32CurrentWorkMode = 0x7A;
    _i32AxisSelect = 0;

    InitUI();

    _pAxisBtnGroup->buttons()[0]->setChecked(true);
    _pWorkModeBtnGroup->buttons()[0]->setChecked(true);
    setParamInfo();

    void (QButtonGroup::*fn)(int) = &QButtonGroup::buttonClicked;

    connect(_pAxisBtnGroup,fn,this,&QGroundTestWidget::onAxisChange);

    connect(_pWorkModeBtnGroup,fn,this,&QGroundTestWidget::onWorkModeChange);

    connect(_pStartTestBtn,&QPushButton::clicked,this,&QGroundTestWidget::onStartTest);


}

/**
* @brief
* @note
*
*/
void QGroundTestWidget::InitUI()
{

    QStringList sAxiNamelist;
    sAxiNamelist<<"方位轴"<<"俯仰轴";//<<"两轴同时";

    _pAxisBtnGroup = new QButtonGroup(this);
    _pWorkModeBtnGroup = new QButtonGroup(this);

    QLabel* plabel = new QLabel("轴选择:",this);
    plabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");

    QHBoxLayout* pHboxlayout = new QHBoxLayout();
    for(int i = 0; i < sAxiNamelist.size();i++)
    {
        QPushButton* pButton = new QPushButton(sAxiNamelist[i],this);
        pButton->setCheckable(true);
        setButtonStyleSheet(pButton);
        _pAxisBtnGroup->addButton(pButton,i);
        pHboxlayout->addWidget(pButton);
    }
    pHboxlayout->addItem(new QSpacerItem(100,1,QSizePolicy::Expanding,QSizePolicy::Expanding));


    QLabel* plabel1 = new QLabel("工作模式:",this);
    plabel1->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");

    QHBoxLayout* pHboxlayout1 = new QHBoxLayout();
    QHBoxLayout* pHboxlayout2 = new QHBoxLayout();
    for(int i = 0; i < _workModeParamMap.keys().size();i++)
    {
        int l_key = _workModeParamMap.keys().at(i);
        QString sName = _workModeParamMap.value(l_key).sName;
        QPushButton* pButton = new QPushButton(sName,this);
        pButton->setCheckable(true);
        setButtonStyleSheet(pButton);
        _pWorkModeBtnGroup->addButton(pButton,l_key);
        if(i/3==0)
        {
            pHboxlayout1->addWidget(pButton);
        }
        else
        {
            pHboxlayout2->addWidget(pButton);

        }

    }


    QVBoxLayout*pVboxlayout = new QVBoxLayout(this);
    pVboxlayout->addWidget(plabel,1);
    pVboxlayout->addLayout(pHboxlayout,1);
    pVboxlayout->addWidget(plabel1,1);
    pVboxlayout->addLayout(pHboxlayout1,1);
    pVboxlayout->addLayout(pHboxlayout2,1);
    pVboxlayout->addWidget(createParamWidget(),4);

}

/**
* @brief
* @note
*
*/
QWidget* QGroundTestWidget::createParamWidget()
{
    QWidget* pwidget = new QWidget(this);
    QVBoxLayout* pVBoxlayout = new QVBoxLayout(pwidget);

    for(int i = 0; i < 4;i++)
    {
        _pParamNameLabel[i] = new QLabel("累加值",this);
        _pParamNameLabel[i]->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");

        _pParamEdit[i] = new QLineEdit(this);
        QUIQss::setTxtQss(_pParamEdit[i], "#DCE4EC", "#1ABC9C");

        QHBoxLayout* pHboxlayout = new QHBoxLayout();
        pHboxlayout->setObjectName(QString::number(i)+"_hbox");

        pHboxlayout->addWidget(_pParamNameLabel[i],1);
        pHboxlayout->addWidget(_pParamEdit[i],2);

        pVBoxlayout->addLayout(pHboxlayout);
    }


    _pStartTestBtn = new QPushButton("开始测试");
    QUIQss::setBtnQss(_pStartTestBtn,"#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");
    QHBoxLayout* pHboxlayout1 = new QHBoxLayout();
    pHboxlayout1->addWidget(_pStartTestBtn,1,Qt::AlignRight);
    pVBoxlayout->addLayout(pHboxlayout1);


    return pwidget;
}

///**
//* @brief
//* @note
//*
//*/
//QWidget* QGroundTestWidget::createParamWidget()
//{
//    QWidget* pwidget = new QWidget(this);

//    _pParamNameLabel = new QLabel("累加值",this);
//    _pParamNameLabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");

//    _pParamEdit = new QLineEdit(this);
//    QUIQss::setTxtQss(_pParamEdit, "#DCE4EC", "#1ABC9C");

//    _pStartTestBtn = new QPushButton("开始测试");
//    _pStartTestBtnGroup->addButton(_pStartTestBtn,0x0);
//    QUIQss::setBtnQss(_pStartTestBtn,"#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");

//    QGridLayout* playout = new QGridLayout(pwidget);
//    playout->addWidget(_pParamNameLabel,0,0,1,1);
//    playout->addWidget(_pParamEdit,0,1,1,2);
//    playout->addWidget(_pStartTestBtn,1,2,1,1);

//    return pwidget;
//}
///**
//* @brief
//* @note
//*
//*/
//QWidget* QGroundTestWidget::createIdentifParamWidget()
//{
//    QWidget* pwidget = new QWidget(this);

//    _pScanFreqLabel = new QLabel("扫描频率",this);
//    _pScanFreqLabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");

//    _pScanFreqEdit = new QLineEdit(this);
//    QUIQss::setTxtQss(_pScanFreqEdit, "#DCE4EC", "#1ABC9C");

//    _pScanAmpLabel = new QLabel("扫描幅度",this);
//    _pScanAmpLabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");

//    _pScanAmpEdit = new QLineEdit(this);
//    QUIQss::setTxtQss(_pScanAmpEdit, "#DCE4EC", "#1ABC9C");

//    _pStartScanBtn = new QPushButton("开始扫频");
//    _pStartTestBtnGroup->addButton(_pStartScanBtn,0x01);
//    QUIQss::setBtnQss(_pStartScanBtn,"#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");

//    QGridLayout* playout = new QGridLayout(pwidget);
//    playout->addWidget(_pScanFreqLabel,0,0,1,1);
//    playout->addWidget(_pScanFreqEdit,0,1,1,2);
//    playout->addWidget(_pScanAmpLabel,1,0,1,1);
//    playout->addWidget(_pScanAmpEdit,1,1,1,2);
//    playout->addWidget(_pStartScanBtn,2,2,1,1);

//    return pwidget;
//}
/**
* @brief
* @note
*
*/
void QGroundTestWidget::setParamInfo()
{
    st_workmodeParam l_param = _workModeParamMap[_i32CurrentWorkMode];
    if(_i32AxisSelect != 2 && l_param.paramCnt == 1)//不是两轴同时
    {
        setHBoxHide(QString::number(0)+"_hbox",false);
        setHBoxHide(QString::number(1)+"_hbox",true);
        setHBoxHide(QString::number(2)+"_hbox",true);
        setHBoxHide(QString::number(3)+"_hbox",true);
        QString sName = QString("%1(%2)").arg(l_param.param1.sParamName).arg(l_param.param1.sUnit);
        _pParamNameLabel[0]->setText(sName);
        _pParamEdit[0]->setToolTip(QString("%1%2-%3%2").arg(l_param.param1.lfParamMin).arg(l_param.param1.sUnit).arg(l_param.param1.lfParamMax));

        _pStartTestBtn->setText("开始测试");

    }
    else if(_i32AxisSelect != 2 && l_param.paramCnt == 2)//两个变量
    {
        setHBoxHide(QString::number(0)+"_hbox",false);
        setHBoxHide(QString::number(1)+"_hbox",false);
        setHBoxHide(QString::number(2)+"_hbox",true);
        setHBoxHide(QString::number(3)+"_hbox",true);

        QString sName = QString("%1(%2)").arg(l_param.param1.sParamName).arg(l_param.param1.sUnit);
        _pParamNameLabel[0]->setText(sName);
        _pParamEdit[0]->setToolTip(QString("%1%2-%3%2").arg(l_param.param1.lfParamMin).arg(l_param.param1.sUnit).arg(l_param.param1.lfParamMax));

        sName = QString("%1(%2)").arg(l_param.param2.sParamName).arg(l_param.param2.sUnit);
        _pParamNameLabel[1]->setText(sName);
        _pParamEdit[1]->setToolTip(QString("%1%2-%3%2").arg(l_param.param2.lfParamMin).arg(l_param.param2.sUnit).arg(l_param.param2.lfParamMax));

        _pStartTestBtn->setText("开始扫描");
    }
    else if(_i32AxisSelect == 2 && l_param.paramCnt == 1)//两轴同时
    {
        setHBoxHide(QString::number(0)+"_hbox",false);
        setHBoxHide(QString::number(1)+"_hbox",false);
        setHBoxHide(QString::number(2)+"_hbox",true);
        setHBoxHide(QString::number(3)+"_hbox",true);

        QString sName = QString("方位-%1(%2)").arg(l_param.param1.sParamName).arg(l_param.param1.sUnit);
        _pParamNameLabel[0]->setText(sName);
        _pParamEdit[0]->setToolTip(QString("%1%2-%3%2").arg(l_param.param1.lfParamMin).arg(l_param.param1.sUnit).arg(l_param.param1.lfParamMax));

        sName = QString("俯仰-%1(%2)").arg(l_param.param1.sParamName).arg(l_param.param1.sUnit);
        _pParamNameLabel[1]->setText(sName);
        _pParamEdit[1]->setToolTip(QString("%1%2-%3%2").arg(l_param.param1.lfParamMin).arg(l_param.param1.sUnit).arg(l_param.param1.lfParamMax));

        _pStartTestBtn->setText("开始测试");

    }
    else if(_i32AxisSelect == 2 && l_param.paramCnt == 2)//两轴同时
    {
        setHBoxHide(QString::number(0)+"_hbox",false);
        setHBoxHide(QString::number(1)+"_hbox",false);
        setHBoxHide(QString::number(2)+"_hbox",false);
        setHBoxHide(QString::number(3)+"_hbox",false);

        QString sName = QString("方位-%1(%2)").arg(l_param.param1.sParamName).arg(l_param.param1.sUnit);
        _pParamNameLabel[0]->setText(sName);
        _pParamEdit[0]->setToolTip(QString("%1%2-%3%2").arg(l_param.param1.lfParamMin).arg(l_param.param1.sUnit).arg(l_param.param1.lfParamMax));

        sName = QString("方位-%1(%2)").arg(l_param.param2.sParamName).arg(l_param.param2.sUnit);
        _pParamNameLabel[1]->setText(sName);
        _pParamEdit[1]->setToolTip(QString("%1%2-%3%2").arg(l_param.param2.lfParamMin).arg(l_param.param2.sUnit).arg(l_param.param2.lfParamMax));

        sName = QString("俯仰-%1(%2)").arg(l_param.param1.sParamName).arg(l_param.param1.sUnit);
        _pParamNameLabel[2]->setText(sName);
        _pParamEdit[2]->setToolTip(QString("%1%2-%3%2").arg(l_param.param1.lfParamMin).arg(l_param.param1.sUnit).arg(l_param.param1.lfParamMax));

        sName = QString("俯仰-%1(%2)").arg(l_param.param2.sParamName).arg(l_param.param2.sUnit);
        _pParamNameLabel[3]->setText(sName);
        _pParamEdit[3]->setToolTip(QString("%1%2-%3%2").arg(l_param.param2.lfParamMin).arg(l_param.param2.sUnit).arg(l_param.param2.lfParamMax));
        _pStartTestBtn->setText("开始扫描");
    }
}

/**
* @brief
* @note
*
*/
void QGroundTestWidget::onAxisChange(int Index)
{
    _i32AxisSelect = Index;
    setParamInfo();
}

/**
* @brief
* @note
*
*/
void QGroundTestWidget::onWorkModeChange(int nIndex)
{
    _i32CurrentWorkMode = nIndex;
    setParamInfo();
}

/**
* @brief
* @note
*
*/
void QGroundTestWidget::setHBoxHide(QString sObjectName,bool bHid)
{
    QHBoxLayout* pHboxlayout = this->findChild<QHBoxLayout*>(sObjectName);
    if(pHboxlayout == nullptr)
    {
        return;
    }

    for (int i = 0; i < pHboxlayout->count(); ++i)
    {
           QWidget* w = pHboxlayout->itemAt(i)->widget();
           if (w != nullptr)
           {
               w->setHidden(bHid);
            }
     }

}


void QGroundTestWidget::setButtonStyleSheet(QPushButton* pBtn)
{

    pBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(245,90,70);;color:white;}"
                "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                "font-size:18px;font-family: 'Microsoft YaHei';}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"

                                );

    pBtn->setMinimumHeight(40);
}
/**
* @brief
* @note
*
*/
void QGroundTestWidget::setButtonGroupEnable(bool bEnable)
{
    for(int i = 0; i < _pAxisBtnGroup->buttons().size();i++)
    {
        if(i == _i32AxisSelect)
        {
            continue;
        }
        _pAxisBtnGroup->buttons()[i]->setEnabled(bEnable);
    }
    for(int i = 0; i < _pWorkModeBtnGroup->buttons().size();i++)
    {
        if(_pWorkModeBtnGroup->id(_pWorkModeBtnGroup->buttons()[i]) == _i32CurrentWorkMode)
        {
            continue;
        }
        _pWorkModeBtnGroup->buttons()[i]->setEnabled(bEnable);
    }

}
/**
* @brief
* @note
*
*/
void QGroundTestWidget::sendParamData()
{
    st_workmodeParam l_param = _workModeParamMap[_i32CurrentWorkMode];

    float lfdata[4];
    for(int i = 0; i < 4; i++)
    {
        lfdata[i] = _pParamEdit[i]->text().toFloat();
    }

    if(l_param.paramCnt == 1 && _i32AxisSelect != 2)//单轴
    {
        fourchar l_fourchar;
        l_fourchar.ui32data= lfdata[0]*l_param.param1.lfCoeff;
        emit signal_startTest(_i32AxisSelect,_i32CurrentWorkMode,l_fourchar.ui32data,0);
    }

    if(l_param.paramCnt == 2 && _i32AxisSelect != 2)//单轴
    {
        fourchar l_fourchar;
        l_fourchar.u16data[1]= lfdata[0]*l_param.param1.lfCoeff;
        l_fourchar.u16data[0]= lfdata[1]*l_param.param2.lfCoeff;
        emit signal_startTest(_i32AxisSelect,_i32CurrentWorkMode,l_fourchar.ui32data,0);
    }

    if(l_param.paramCnt == 1 && _i32AxisSelect == 2)//双轴
    {
        int data[2];
        data[0]= lfdata[0]*l_param.param1.lfCoeff;
        data[1]= lfdata[1]*l_param.param2.lfCoeff;

        emit signal_startTest(_i32AxisSelect,_i32CurrentWorkMode,data[0],data[1]);
    }

    if(l_param.paramCnt == 2 && _i32AxisSelect == 2)//双轴
    {
        fourchar l_fourchar[2];

        l_fourchar[0].u16data[1]= lfdata[0]*l_param.param1.lfCoeff;
        l_fourchar[0].u16data[0]= lfdata[1]*l_param.param1.lfCoeff;

        l_fourchar[1].u16data[1]= lfdata[2]*l_param.param2.lfCoeff;
        l_fourchar[1].u16data[0]= lfdata[3]*l_param.param2.lfCoeff;

        emit signal_startTest(_i32AxisSelect,_i32CurrentWorkMode,l_fourchar[0].ui32data,l_fourchar[1].ui32data);
    }


}

/**
* @brief
* @note
*
*/
void QGroundTestWidget::onStartTest()
{
    st_workmodeParam l_param = _workModeParamMap[_i32CurrentWorkMode];

    QString sText = _pStartTestBtn->text();
    if(sText.contains("开始") == true)//开始测试
    {
        setButtonGroupEnable(false);
        sendParamData();

        _pStartTestBtn->setText("停止测试");
        if(l_param.paramCnt == 2)
        {
            _pStartTestBtn->setText("停止扫描");
        }
    }
    else//停止测试
    {
        setButtonGroupEnable(true);
        _pStartTestBtn->setText("开始测试");
        if(l_param.paramCnt == 2)
        {
            _pStartTestBtn->setText("开始扫描");
        }
    }
}

/**
* @brief 测试完成
* @note
*
*/
void QGroundTestWidget::StopTest()
{
    st_workmodeParam l_param = _workModeParamMap[_i32CurrentWorkMode];

    _pStartTestBtn->setText("开始测试");
    if(l_param.paramCnt == 2)
    {
        _pStartTestBtn->setText("开始扫描");
    }
    setButtonGroupEnable(true);

    //数据处理

}
